import { Component, OnInit } from '@angular/core';
import { ZoneService, ZoneDetail, ZoneResource } from '../../services/zone.service';
import { ActivatedRoute } from '@angular/router';
import { BehaviorSubject, ReplaySubject, merge, of } from 'rxjs';
import { APIBase } from 'app/modules/base/api-base';
import { MySelection } from 'app/modules/base/selection';
import { Personnel } from 'app/modules/personnel/services/personnel.service';
import { Computer } from 'app/modules/computer/services/computer.service';
import { SoftwareInfo } from 'app/modules/software/services/software.service';
import { map } from 'rxjs/operators';
import { MatDialog } from '@angular/material';
import { DefaultPermissionComponent } from '../default-permission/default-permission.component';

@Component({
	selector: 'm-permission-group',
	templateUrl: './permission-group.component.html',
	styleUrls: ['./permission-group.component.scss'],
})
export class PermissionGroupComponent implements OnInit {
	constructor(private route: ActivatedRoute, private dialog: MatDialog, private zoneService: ZoneService) { }
	fullscreen = false;
	userId: number;
	tabs = [
		{ label: '人员视角', value: 'user' },
		{ label: '终端视角', value: 'pc' },
		{ label: '软件视角', value: 'software' },
	];
	list: ZoneResource[] = ['user', 'pc', 'software'];
	resources = this.list.map((r, index) => {
		return {
			name: r,
			index: index,
			resource$: new ReplaySubject<APIBase>(1),
			selection: new MySelection<{ id: number }>('id', [], false),
		};
	});
	path: [ZoneResource, number][] = [];
	zone: ZoneDetail;
	ngOnInit() {
		this.zone = this.route.snapshot.data['zone'];
		this.change('user');
	}
	change(value: ZoneResource) {
		// 视角排序
		const list = {
			user: ['user', 'pc', 'software'],
			pc: ['pc', 'user', 'software'],
			software: ['software', 'user', 'pc'],
		}[value];

		// 视角资源生成
		this.resources = list.map((r: ZoneResource, index) => {
			return {
				name: r,
				index: index,
				resource$: new ReplaySubject<APIBase>(1),
				selection: new MySelection<{ id: number }>('id', [], false),
			};
		});
		//加载一级资源
		this.resources[0].resource$.next(
			this.zoneService.resource(this.zone.id, ('select_' + this.resources[0].name) as any),
		);

		const changes = this.resources.map(r => r.selection.change.pipe(map(() => r)));
		// 选中后，加载相关下级资源
		merge(...changes).subscribe(resource => {
			const selected = resource.selection.selected[0];
			// 清除下级选中状态
			this.resources.slice(resource.index + 1).forEach(r => r.selection.clear());
			this.resources.slice(resource.index + 1).forEach(r => r.resource$.next(null));
			if (!selected) {
				return;
			}
			// 构建资源查询路径
			this.path = [...this.path.slice(0, resource.index), [resource.name, selected.id]];
			//获取用户id
			this.path.forEach(item => {
				if (item[0] == "user") {
					this.userId = item[1]
				}
			})
			// 查询已选中的下级资源
			const next = this.resources[resource.index + 1];
			next.resource$.next(this.zoneService.auth(this.zone.id, next.name, [...this.path]));
		});
	}

	previewConfig(pc_id) {
		const dialogRef = this.dialog.open(DefaultPermissionComponent);
		// dialogRef.componentInstance.config$ = of(config);
		dialogRef.componentInstance.readonly = true;
		dialogRef.componentInstance.label = '用户终端配置';
		dialogRef.componentInstance.userId = this.userId;
		dialogRef.componentInstance.pcId = pc_id;
	}
}
